package com.qc.dao.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.sql.DataSource;

import org.springframework.stereotype.Repository;

import com.qc.common.Constants;
import com.qc.common.DBHandler;
import com.qc.common.DBUtil;
import com.qc.dao.UserWalletDao;
import com.qc.entity.UserEntity;

@Repository
public class UserWalletDaoImpl implements UserWalletDao {

	@Resource
	private DataSource dataSource;
	
	@Override
	public long updateWalletPwd(String pwd, long userId) {
		DBHandler dbHandler = new DBHandler();
		List<Object> l = new ArrayList<Object>();
		l.add(pwd);
		l.add(userId);

		long i = 0;
		try {
			i = dbHandler.update(DBUtil.getSql(Constants.UPDATE_USER_WALLET_PWD), l, dataSource);
			return i;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return 0;
	}

	@Override
	public UserEntity getUserInfoByWalletPwdAndUserId(String walletPwd, long userId) {
		DBHandler dbHandler = new DBHandler();
		List<Object> l = new ArrayList<Object>();
		l.add(userId);
		l.add(walletPwd);
		try {
			dbHandler.rs = dbHandler.query(DBUtil.getSql(Constants.GET_USER_INFO_BYUSERID_AND_WALLETPWD), l, dataSource);
			if(dbHandler.rs.next()){

				UserEntity entity = UserEntity.getInstance(dbHandler.rs);
				return entity;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}  finally {
			dbHandler.closeAll();
		}
		return null;
	}

}
